package org.bronycorps.mlptss.screen;

import java.util.ArrayList;

import org.bronycorps.mlptss.domain.controller.GameController;
import org.bronycorps.mlptss.domain.model.Character;
import org.bronycorps.mlptss.domain.model.type.PlayerType;
import org.bronycorps.mlptss.manager.CharacterFactory;
import org.bronycorps.mlptss.manager.ScreenLoader;
import org.bronycorps.mlptss.manager.preferences.AssetsConfiguration.CharacterConfiguration;
import org.bronycorps.mlptss.utils.MenuInputListener;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class CharacterSelectionScreen extends AbstractMenuScreen {

	private TextureAtlas atlas;
	private Group attackBar;
	private ArrayList<CharacterConfiguration> characters;
	private BitmapFont charNameFont;
	private Label charNameLabel;
	private Image currentCharacterPicture;
	private int selectedCharacterId;
	private Group speedBar;

	public CharacterSelectionScreen() {
		super();

		this.atlas = this.manager.get(
				this.config.convertPath(this.menuConfig.getMenuItems()),
				TextureAtlas.class);
		this.charNameFont = this.manager
				.get(this.config.convertPath(this.menuConfig
						.getCharacterNameFont()), BitmapFont.class);

		this.characters = this.config.getCurrentMod().getAssetsConfiguration()
				.getCharacters();
		this.selectedCharacterId = 0;
	}

	private TextButton createButton(String caption, int x, int y, int width,
			int height, InputListener callback) {
		TextButton button = new TextButton(caption, getSkin());
		button.addListener(callback);
		button.size(width, height);
		button.setPosition(x, y);

		return button;
	}

	private void setCharacter(int id) {
		this.selectedCharacterId = id;
		CharacterConfiguration charConfig = this.characters
				.get(this.selectedCharacterId);

		this.charNameLabel.setText(charConfig.getName());

		TextureAtlas charAtlas = this.manager.get(
				this.config.convertPath(charConfig.getAssetAtlas()),
				TextureAtlas.class);

		AtlasRegion regionDrawable = charAtlas
				.findRegion("character-selection");
		this.currentCharacterPicture.setDrawable(new TextureRegionDrawable(
				regionDrawable));

		int width = regionDrawable.getRegionWidth();
		int height = regionDrawable.getRegionHeight();

		this.currentCharacterPicture.setSize(width, height);
		this.currentCharacterPicture.setPosition(LevelScreen.STAGE_WIDTH / 2
				- width / 2, LevelScreen.STAGE_HEIGHT / 2 - height / 2);

		// change specs
		AtlasRegion dotRegion = this.atlas.findRegion("skillDot");
		this.attackBar.clearChildren();
		this.speedBar.clearChildren();
		for (int i = 0; i < charConfig.getAttack(); i++) {
			Image dot = new Image(dotRegion);
			dot.setPosition(i * (dotRegion.getRegionWidth() + 5), 0);
			this.attackBar.addActor(dot);
		}
		for (int i = 0; i < charConfig.getSpeed(); i++) {
			Image dot = new Image(dotRegion);
			dot.setPosition(i * (dotRegion.getRegionWidth() + 5), 0);
			this.speedBar.addActor(dot);
		}

	}

	@Override
	public void show() {
		Gdx.input.setInputProcessor(this.stage);

		// add background
		addDefaultBackground();

		// add buttons
		this.stage.addActor(createButton("Back", 5, 550, 50, 10,
				new MenuInputListener() {
					@Override
					public void touchUp(InputEvent event, float x, float y,
							int pointer, int button) {
						super.touchUp(event, x, y, pointer, button);
						ScreenLoader.getInstance().loadScreen(new MenuScreen());
					}
				}));

		this.stage.addActor(createButton("Start", 925, 5, 50, 10,
				new MenuInputListener() {
					@Override
					public void touchUp(InputEvent event, float x, float y,
							int pointer, int button) {
						super.touchUp(event, x, y, pointer, button);

						// prepare the selected character
						Character selectedCharacter = CharacterFactory
								.getCharacter(CharacterSelectionScreen.this.characters
										.get(CharacterSelectionScreen.this.selectedCharacterId));
						GameController.getInstance().selectPlayer(0,
								selectedCharacter, PlayerType.HUMAN);

						GameController.getInstance().newGame();
					}
				}));

		Image prevCharacterBtn = new Image(this.atlas.findRegion("arrow-left"));
		prevCharacterBtn.setPosition(140, 237);
		prevCharacterBtn.addListener(new MenuInputListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				setCharacter((CharacterSelectionScreen.this.selectedCharacterId
						+ CharacterSelectionScreen.this.characters.size() - 1)
						% CharacterSelectionScreen.this.characters.size());
			}

		});
		this.stage.addActor(prevCharacterBtn);

		Image nextCharacterBtn = new Image(this.atlas.findRegion("arrow-right"));
		nextCharacterBtn.setPosition(793, 237);
		nextCharacterBtn.addListener(new MenuInputListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				setCharacter((CharacterSelectionScreen.this.selectedCharacterId + 1)
						% CharacterSelectionScreen.this.characters.size());
			}

		});
		this.stage.addActor(nextCharacterBtn);

		// add character name
		LabelStyle style = new LabelStyle(this.charNameFont, new Color(1, 1, 1,
				1));
		this.charNameLabel = new Label("Fluttershy", style);
		this.charNameLabel.setPosition(370, 510);
		this.charNameLabel.setAlignment(Align.center);
		this.stage.addActor(this.charNameLabel);

		// add specs box
		Image specs = new Image(this.atlas.findRegion("specs"));
		specs.setPosition(800, 480);
		this.stage.addActor(specs);

		this.attackBar = new Group();
		this.attackBar.setPosition(873, 490);
		this.stage.addActor(this.attackBar);
		this.speedBar = new Group();
		this.speedBar.setPosition(873, 534);
		this.stage.addActor(this.speedBar);

		// add character

		this.currentCharacterPicture = new Image();
		this.currentCharacterPicture.setPosition(LevelScreen.STAGE_WIDTH / 2,
				LevelScreen.STAGE_HEIGHT / 2);

		setCharacter(this.selectedCharacterId);

		this.stage.addActor(this.currentCharacterPicture);

	}

}
